package mysql

import (
	"gorm.io/gorm"
	"video-hub/videorpc/videorpc"
)

type Collect struct {
	gorm.Model
	VideoId int64 `gorm:"type:int(11);not null;comment:视频ID"`
	UserId  int64 `gorm:"type:int(11);not null;comment:上传用户ID"`
}

func (c *Collect) FindUserIdAndVideoId(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Where("video_id=? and user_id =?", id, id2).Find(c).Error
}

func (c *Collect) CollectAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Collect) DelCollect(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Model(&Collect{}).Where("user_id =? and video_id =?", id, id2).Delete(c).Error
}

/*
	SELECT
	 users.nick_name username,
	 videos.id video_id,
	 videos.video_title,
	 videos.video_category,
	 videos.video_link,
	 videos.cover_link,
	 videos.video_intro,
	 topics.topic_name,
	 videos.play_num,
	 videos.like_num,
	 videos.collect_num,
	 videos.comment_num

FROM

	 collects
	LEFT JOIN users ON collects.user_id = users.id

LEFT JOIN videos ON collects.video_id=videos.id
LEFT JOIN topics ON videos.topic_id=topics.id
*/
func CollectList(db *gorm.DB, in *videorpc.CollectListRequest) []*videorpc.CollectList {
	var list []*videorpc.CollectList
	db.Debug().Model(&Collect{}).
		Select(
			"users.nick_name username",
			"videos.id video_id",
			"videos.video_title",
			"videos.video_category",
			"videos.video_link",
			"videos.cover_link",
			"videos.video_intro",
			"topics.topic_name",
			"videos.play_num",
			"videos.like_num",
			"videos.collect_num",
			"videos.comment_num",
		).
		Joins("LEFT JOIN users ON collects.user_id = users.id").
		Joins("LEFT JOIN videos ON collects.video_id=videos.id").
		Joins("LEFT JOIN topics ON videos.topic_id=topics.id").
		Where("collects.user_id=?", in.UserId).
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list)
	return list
}
